package zhtt.oneToMany6_casedelete;

import org.junit.Test;
import zhtt.JunitTestUtil;

/**
 * Created by zhtt on 2017/4/11.
 * country的配置文件中，将控制返回设为true（inverse="true"）
 * 此时保存时，多方表中一方的id为空
 */
public class JunitTest6 extends JunitTestUtil {

    @Test
    /**
     * 1、根据id查询【country】
     * 2、根据id查询【minister】
     * 3、根据country_id查询【minister】
     * 4、更新【minister】的country_id值为null
     * 5、删除【minister】
     * 相互set
     */
    public void delete(){
        try{
            session.beginTransaction();
            Country c=session.get(Country.class,2);
            Minister minister=session.get(Minister.class,5);
            //从集合中把minister删除，即minister就变成了orphan,如果cascade设置了delete-orphan，则会从数据库中删除minister
            c.getMinisterSet().remove(minister);
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
/**
 Hibernate:
 select
 country0_.id as id1_1_0_,
 country0_.name as name2_1_0_
 from
 zhtt07_country country0_
 where
 country0_.id=?
 Hibernate:
 select
 minister0_.id as id1_3_0_,
 minister0_.name as name2_3_0_,
 minister0_.country_id as country_3_3_0_
 from
 zhtt07_minister minister0_
 where
 minister0_.id=?
 Hibernate:
 select
 ministerse0_.country_id as country_3_3_0_,
 ministerse0_.id as id1_3_0_,
 ministerse0_.id as id1_3_1_,
 ministerse0_.name as name2_3_1_,
 ministerse0_.country_id as country_3_3_1_
 from
 zhtt07_minister ministerse0_
 where
 ministerse0_.country_id=?
 Hibernate:
 update
 zhtt07_minister
 set
 country_id=null
 where
 country_id=?
 Hibernate:
 delete
 from
 zhtt07_minister
 where
 id=?
 */
